package com.stsdai.portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stsdai.cart.service.CartService;
import com.stsdai.manager.pojo.Cart;
import com.stsdai.manager.pojo.Order;
import com.stsdai.manager.pojo.Receiver;
import com.stsdai.manager.pojo.User;
import com.stsdai.manager.service.ReceiverService;
import com.stsdai.portal.util.CookieUtils;
import com.stsdai.order.service.OrderService;
import com.stsdai.sso.service.UserService;

@Controller
@RequestMapping("order")
public class OrderController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private CartService cartService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private ReceiverService receiverService;
	
	//http://www.taotao.com/order/create.html
	@RequestMapping("create")
	public String create(HttpServletRequest request, Model model){
		
		//先通过Cookie获取Ticket
//		String ticket = CookieUtils.getCookieValue(request, "STSD_TICKET", true);
//		
//		//根据Ticket来获取用户
//		User user = this.userService.queryUserByTicket(ticket);
		//查询这个购物车集合：根据用户ID从redis中
		
		//用户能进入这个Controller那就说明一定是登录状态，那么直接从request中获取用户信息
		User user = (User)request.getAttribute("user");
		
		List<Cart> list = null;
		if(user != null){
			list = this.cartService.queryCartByUserId(user.getId());
		}
		//页面中需要什么数据？
		model.addAttribute("carts", list);
		List<Receiver> receiverList = receiverService.queryReceiver(user.getId());
		model.addAttribute("receiverList", receiverList);
		
		return "order-cart";
	}
	
	//http://www.taotao.com/service/order/submit
	/**
	 * type : "POST",
		dataType : "json",
		url : "/service/order/submit",
		data : $("#orderForm").serialize(),
		cache : false,
		success : function(result) {
			if(result.status == 200){
				location.href = "/order/success.html?id="+result.data;
			}else{
				$("#submit_message").html("订单提交失败，请稍后重试...").show();
			}
		}
	 */
	@RequestMapping(value="submit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> submit(Order order, HttpServletRequest request){
		
		//通过OrderService来保存Order订单的基本信息
		//一定是放在一个方法中保存！！！
		//保存订单商品的时候出错了！
		//现在没有订单ID是因为ID必须是自定义的而且是可读性比较高的！redis中的一个自增长：肯定不会重复
		//获取用户信息
		User user = (User)request.getAttribute("user");
		order.setUserId(user.getId());
		order.setBuyerNick(user.getUsername());
		
		String orderId = this.orderService.saveOrder(order);
		
		//返回页面数据
		Map<String, Object> map = new HashMap<>();
		map.put("status", 200);
		map.put("data", orderId);
		
		return map;
	}
	
	
	///order/success.html?id=订单ID
	@RequestMapping("success")
	public String success(String id, Model model){
		
		//根据订单ID来查询订单信息
		Order order = this.orderService.queryOrderById(id);
		
		//计算物流时间信息: 预计物流两天后送达
		String date = new DateTime().plusDays(2).toString("yyyy-MM-dd HH:mm:ss SSS");
		
		//封装返回数据
		model.addAttribute("order", order);
		model.addAttribute("date", date);
		
		return "success";
	}
	
	
}
